let s = "aacabdkacaa";


var longestPalindrome = function (s) {
    if (s.length == 1) return s;
    let result = s[0];
    for (let i = 0; i < s.length; i++) {
        for (let j = i + 1; j < s.length; j++) {
            if (s[i] == s[j]) {
                let flag = isPalindrome(s.slice(i, j + 1));
                if (flag && j + 1 > result.length) {
                    result = s.slice(i, j + 1);
                    if (i == 1) console.log(result);

                    if (result.length == s.length) return result;
                }
            }
        }
    }
    console.log(result);

    return result;
};

function isPalindrome(str) {
    // 预处理：去除非字母数字字符并转为小写
    const cleaned = str.replace(/[^a-z0-9]/gi, '').toLowerCase();
    let left = 0, right = cleaned.length - 1;
    while (left < right) {
        if (cleaned[left] !== cleaned[right]) return false;
        left++;
        right--;
    }
    return true;
}

longestPalindrome(s);